//
//  MCCreatePlanVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/19.
//

import UIKit
import SnapKit
import QMUIKit
import Toast_Swift
import MJRefresh


class MCCreatePlanVc: MCBaseVc {
    let mcCreatePlanIdentifier = "mcCreatePlanIdentifier"
    
    var tableBgView: UIView!
    var createPlanTableView:UITableView!
    
    private var dataSource:Array = [HomeRecommandRecord?]()
    
    var current:Int = 1
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        self.navigationItem.title = "我创建的计划"
        
        tableBgView = UIView()
        tableBgView.layer.cornerRadius = 6
        view.addSubview(tableBgView)
        tableBgView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.bottom.equalTo(0)
        }
        createPlanTableView = UITableView.init(frame: CGRect.zero, style: .plain)
        createPlanTableView.delegate = self
        createPlanTableView.dataSource = self
        tableBgView.addSubview(createPlanTableView)
        createPlanTableView.snp.makeConstraints { make in
//            make.left.equalTo(view.snp.left).offset(0)
//            make.right.equalTo(view.snp.right).offset(0)
//            make.top.equalTo(view.snp.top).offset(0)
//            make.bottom.equalTo(view.snp.bottom).offset(0)
            make.edges.equalTo(0)
        }
        self.createPlanTableView.rowHeight = 50
        self.createPlanTableView.separatorStyle = .none
        createPlanTableView.register(UINib.init(nibName: "MCPlanTableCell", bundle: Bundle.main), forCellReuseIdentifier: mcCreatePlanIdentifier)
        
        
        self.createPlanTableView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            self.current = 1
            self.createPlanListData(current: self.current)
        })
        
        self.createPlanTableView.mj_header?.beginRefreshing()
        
        
        self.createPlanTableView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: {
            self.createPlanListData(current: self.current)
        })
        
        
    }
    
    
    func createPlanListData(current:Int?) -> Void {
        NetWorkRequest(MCCreatePlanApi.recommandPlan(current: current)) { ResponseModel in
            self.createPlanTableView.mj_footer?.endRefreshing()
            self.createPlanTableView.mj_header?.endRefreshing()
            let createModel = HomeRecommandInfoListModel.init(JSONString: ResponseModel.dataString)
            if current == 1 {
                self.dataSource.removeAll()
                self.dataSource.append(contentsOf: createModel?.records ?? [])
                
            }else{
                self.current += 1
                
                self.dataSource.append(contentsOf: createModel?.records ?? [])
            }
            self.createPlanTableView.reloadData()
        } failureCallback: { ResponseModel in
            self.createPlanTableView.mj_footer?.endRefreshing()
            self.createPlanTableView.mj_header?.endRefreshing()
            self.view.makeToast("网络失败",position: .center)
        }
    }
    
    
    
}

extension MCCreatePlanVc:UITableViewDataSource,UITableViewDelegate {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 74
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:MCPlanTableCell = tableView.dequeueReusableCell(withIdentifier: mcCreatePlanIdentifier, for: indexPath) as! MCPlanTableCell
        
        let recordsList = self.dataSource[indexPath.row]
        
        cell.HeaderIconImageView.kf.setImage(with:URL(string: recordsList?.planUrl ?? ""))
        cell.titleInfoLabel.text = recordsList?.planName ?? ""
        
        
       for subView  in cell.LabelLayoutView.subviews {
           subView.removeFromSuperview()
        }
        
        
        let paddingLabel = QMUIButton.init()
        paddingLabel.backgroundColor = UIColor.qmui_color(withHexString:"#35DE94")?.withAlphaComponent(0.1)
        paddingLabel.setTitleColor( UIColor.qmui_color(withHexString: "#35DE94"), for: .normal)
        paddingLabel.setTitle("L\(String(describing:(recordsList?.planLevel ?? 0)))", for: .normal)
        paddingLabel.contentEdgeInsets = UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10)
        paddingLabel.layer.cornerRadius = 5
        paddingLabel.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 10)
        paddingLabel.layer.masksToBounds = true
        cell.LabelLayoutView.addSubview(paddingLabel)
        
        
        
        return cell
    }
    
    
}
